#!/bin/bash

# A script that creates a compiled profiler in a subdirectory:
#   - create subdirectory (w/name of model)
#   - run compile on model and save model .h and .o to subdirectory
#   - copy c++ and cmake files to subdirectory

# Usage: make_profiler.sh model_file.ell [output_directory] <compile options>
#
# Note: currently this script must be run from the ELL build directory, and the model file must reside in the ELL build directory

set -e

script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

pushd .
# Capture all arguments into a bash array
all_args=("$@")

if [ "${all_args}" == "" ] ; then
  echo "Usage make_profiler.sh model_file.ell [output_directory] <compile options>"
  exit
fi

# Set some defaults
parallel="false"
target="host"
cleanup=true

# Extract target value and parallel flag (without removing them from the argument list)
for arg in "${all_args[@]}"; do
    if [ "${prev_arg}" == "--target" ] ; then
        target=$arg
    fi

    if [ "${arg}" == "--parallelize" ] || [ "${arg}" == "-par" ]; then
        parallel=true
    fi
    prev_arg=$arg
done

echo "Target: ${target}"
echo "Parallel: ${parallel}"

# Get model filename
model_file=$1
model_name="${model_file%.*}"
if [ "${model_file: -6}" == ".model" ] ; then
    model_option="-imf"
else
    model_option="-imap"
fi
shift

# Get output directory (if specified)
profiler_directory=$model_name"_profiler"
next_opt=$1
if [ "${next_opt}" != "" ] && [ "${next_opt:0:1}" != "-" ] ; then
    profiler_directory=$next_opt
    shift
fi

ir_option="--ir"
next_opt=$1
if [ "${next_opt}" != "" ] && [ "${next_opt}" == "--no_opt" ] ; then
    ir_option=""
    shift
fi

# Platform-specific options
compile_opts=""
generic_llc_opts="-relocation-model=pic"
pi3_llc_opts="-relocation-model=pic -mtriple=armv7-linux-gnueabihf -mcpu=cortex-a53"
pi0_llc_opts="-relocation-model=pic -mtriple=arm-linux-gnueabihf -mcpu=arm1176jzf-s"
llc_opts=$generic_llc_opts
if [ ${target} == 'pi3' ] ; then
    llc_opts=$pi3_llc_opts
fi
if [ ${target} == 'pi0' ] ; then
    llc_opts=$pi0_llc_opts
fi
if [ ${target} == 'host' ] ; then
    compile_opts="--positionIndependentCode"
fi

echo "Compiling model profilers to ${profiler_directory}"
mkdir -p $profiler_directory
@COMPILE_EXECUTABLE@ $model_option $model_file --profile ${ir_option} --objectCode ${compile_opts} --header -od $profiler_directory -ob compiled_model $@
@COMPILE_EXECUTABLE@ $model_option $model_file ${ir_option} --objectCode ${compile_opts} --header -od $profiler_directory -ob compiled_model_noprofile $@

cd $profiler_directory
if [ "${ir_option}" != "" ] ; then
@OPT_EXECUTABLE@ compiled_model.ll -o compiled_model_opt.ll -O3 -S
@LLC_EXECUTABLE@ compiled_model_opt.ll -o compiled_model_opt.o -O3 --filetype=obj $llc_opts

@OPT_EXECUTABLE@ compiled_model_noprofile.ll -o compiled_model_noprofile_opt.ll -O3 -S
@LLC_EXECUTABLE@ compiled_model_noprofile_opt.ll -o compiled_model_noprofile_opt.o -O3 --filetype=obj $llc_opts
fi

cp ${script_dir}/../tools/utilities/profile/CompiledProfile_main.cpp .
cp ${script_dir}/../tools/utilities/profile/CompiledExerciseModel_main.cpp .
cp ${script_dir}/../tools/utilities/profile/ProfileReport.h .
cp ${script_dir}/../tools/utilities/profile/ProfileReport.cpp .
cp ${script_dir}/../tools/utilities/profile/OpenBLASSetup.cmake .
cp ${script_dir}/../tools/utilities/profile/build_and_run.sh .

cp ${script_dir}/../tools/utilities/profile/CMakeLists-device-parallel.txt.in ./CMakeLists.txt

if [ "${cleanup}" == "true" ] ; then
    rm -rf ./*.ll
    rm -rf ./*.bc
fi

popd
